//
//  HMVisitorView.swift
//  HMWeiBo
//
//  Created by Apple on 16/7/28.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
// 导入snapkit
import SnapKit

/*
    OC
    1. 定义协议
    2. 声明代理属性
    3. 判断可以不可以运行
    4. 调用代理: 设置代理,遵守代理协议,实现代理方法
*/

protocol HMVisitorViewDelegagte: NSObjectProtocol {
    
    // 方法
    func didLoginMethod()
    
    func didRegisterMethod()
    
}

class HMVisitorView: UIView {

    // OC里的代理用 weak修饰,swift同样用weak
    weak var delegate: HMVisitorViewDelegagte?
    
    
    // 自定义视图三部曲
    
    // 第一步: 重写父类的构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 第二步: 定义一个添加子类和设置子类控件约束的方法
    private func setupUI(){
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
        // 添加到视图
        
        addSubview(turnImageView)
        addSubview(maskImageView)
        addSubview(homeIconImageView)
        
        
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 设置约束
        
        // 蒙版的约束
        maskImageView.snp_makeConstraints { (make) -> Void in
            // snapKit equalTo(self.snp_center) 注意加 snp_center
            make.center.equalTo(self.snp_center)
        }
        
        // 小房子
        homeIconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        turnImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        // 提示语
        /*
            先水平和垂直,然后再 左 上 右 下,最后宽和高
        */
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(turnImageView.snp_bottom).offset(10)
            make.width.equalTo(216)
        }
        
        // 注册按钮 -- 一般的约束是 4个
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel.snp_right)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
    }
    
    // MARK:-- 首页的动画效果
    func startAnimation(){
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        // toValue
        animation.toValue = 2 * M_PI
        
        // 次数和时间
        animation.repeatCount = MAXFLOAT // 永远转
        animation.duration = 10
        
        //  当我们切换视图的时候,当动画完成之后,会被移除
        animation.removedOnCompletion = false
        
        // 添加到layer里
        turnImageView.layer.addAnimation(animation, forKey: nil)
        
    }
    //MARK:-- 对外提供一个方法,来改变视图
    // isHome: Bool = false 是给一个默认值
    func setupInfo(imageName: String,message: String,isHome: Bool = false){
        
        homeIconImageView.image = UIImage(named: imageName)
        messageLabel.text = message
        if isHome {
            turnImageView.hidden = false
            startAnimation()
        } else {
            turnImageView.hidden = true
            
        }
        

    }
    
    
    
    //MARK:-- 按钮的点击
    func register(){
        
        print("按钮点击了")
        // view不能 model一个视图
        
        // OC里我们需要判断一下,这个代理能不能响应,swift中因为delegate是可选
        // delegate 如果是nil 相对于一句 空
        // delegate 有值,就相当于设置了代理
        delegate?.didRegisterMethod()
        
    }
    
    func login(){
        
        print("登录")
        delegate?.didLoginMethod()
    }
    
    //第三步: 懒加载控件
    //MARK:-- 懒加载控件

    


    ///蒙版
    private lazy var maskImageView: UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return img
    }()
    
    ///首页的小房子
    private lazy var homeIconImageView: UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named:"visitordiscover_feed_image_house")
        
        return img
        
    }()
    
    ///转动图片
    private lazy var turnImageView: UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return img
    }()

    
    /// 提示文字
    private lazy var messageLabel: UILabel = {
    
        let label = UILabel()
        
        // 设置属性
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        label.text = "关注一些人"
        
        label.textAlignment = .Center
        
        return label
    }()
    
    ///注册按钮
    private lazy var registerButton: UIButton = {
    
        let button = UIButton()
        
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        // 设置背景
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        // 添加点击事件
        button.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()
    
    ///注册按钮
    private lazy var loginButton: UIButton = {
        
        let button = UIButton()
        
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        // 设置背景
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        // 添加点击事件
        button.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()
}
